package com.car.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ReadExcel {
	public static void main(String args[]) {
		ReadExcel read = new ReadExcel();
		read.importCsv(new File("D:/mydata/uploadoa/doc/400已接电话20160822-20160827.csv"));
    }
	
	public static void readExcel(String pathname, PrintWriter out) {
        try {
            //打开文件
            Workbook book = Workbook.getWorkbook(new File(pathname)) ;
            //取得第一个sheet
            Sheet sheet = book.getSheet(0);
            //取得行数
            int rows = sheet.getRows();
            for(int i = 0; i < rows; i++) {
                Cell [] cell = sheet.getRow(i);
                for(int j=0; j<cell.length; j++) {
                    //getCell(列，行)
                    out.print(sheet.getCell(j, i).getContents());
                    out.print("&nbsp;");
                }
                out.println("<br/>");
            }
            //关闭文件
            book.close();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
	
	
	public static void readExcelByone(String url){
		 try {
	         Workbook book = Workbook.getWorkbook(new File(url));
	         // 获得第一个工作表对象
	         Sheet sheet = book.getSheet(0);
	         // 得到第一列第一行的单元格
	         Cell cell1 = sheet.getCell(0, 0);
	         String result = cell1.getContents();
	         System.out.println(result);
	         book.close();
	    } catch (Exception e) {
	         System.out.println(e);
	    }
	}
	
	//对.csv文件读取
	public static List<String> importCsv(File file){
        List<String> dataList=new ArrayList<String>();
        BufferedReader br=null;
        try { 
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
            String line = ""; 
            while ((line = br.readLine()) != null) { 
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        return dataList;
    }
	/**
	 * @Description:输出流文件（缓存出来的数据）
	 * @author: Hqd
	 * @date: 2017年11月28日 下午5:05:23 
	 * @return
	 */
	public static List<String> importCsv(InputStream is){
        List<String> dataList=new ArrayList<String>();
        BufferedReader br=null;
        try { 
            br = new BufferedReader(new InputStreamReader(is,"GBK"));
            String line = ""; 
            while ((line = br.readLine()) != null) { 
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        return dataList;
    }
	
	
}
